import { defineStore } from 'pinia'
import {postReq,getRequest} from '@/utils/request'
import uri from '@/api/uri'
import cache from '@/utils/cache'
import { useRoute } from 'vue-router'
const route = useRoute()

export const userStore = defineStore('userStore', {

    state: () => ({
		user: {
			id: '',
			username: '',
			email: '',
			avatar: '',
			superAdmin: 0,
			orgId:''
		},
		token: cache.getToken(),
		//权限
		authorities : {},
		//opr 与url的映射
		oprUrlMap:{} as any,
	}),

    actions: {
		
        setAuthorities(val:any){
			this.authorities = val
		},

		setToken(val: any) {
			this.token = val
			cache.setToken(val)
		},

		setUser(val: any) {
			this.user = val
		},

		setOprUrlMap(val: any){
			this.oprUrlMap = val
		},

		//获取token
		async getToken(loginForm: any) {
			const res = await postReq(uri.getOauth2Token,loginForm)
			this.setToken(res.data.access_token)
		},

		// 获取用户信息
		async getUserInfoAction() {
            const res: any = await getRequest(uri.getSystemUser)
			this.setUser(res.data.user)
			this.setAuthorities(res.data.userOpr)
		},

		// 用户退出
		async logoutAction() {
			//await useLogoutApi()

			// 移除 token
			this.setToken(null)
		}
        
	}



})